﻿using System;

namespace Communication.Message.Buffer
{
	// Token: 0x0200002F RID: 47
	internal class DynamicMessageBuffer : AbstractMessageBuffer
	{
		// Token: 0x0600016D RID: 365 RVA: 0x00004F88 File Offset: 0x00003188
		public DynamicMessageBuffer(int estimatedLength) : this(ByteOrder.BIG_ENDIAN, estimatedLength)
		{
		}

		// Token: 0x0600016E RID: 366 RVA: 0x00004F92 File Offset: 0x00003192
		public DynamicMessageBuffer(ByteOrder endianness, int estimatedLength) : this(endianness, estimatedLength, HeapMessageBufferFactory.GetInstance(endianness))
		{
		}

		// Token: 0x0600016F RID: 367 RVA: 0x00004FA4 File Offset: 0x000031A4
		public DynamicMessageBuffer(ByteOrder endianness, int estimatedLength, MessageBufferFactory factory)
		{
			if (estimatedLength < 0)
			{
				throw new ArgumentException("estimatedLength: " + estimatedLength);
			}
			if (factory == null)
			{
				throw new ArgumentNullException("factory");
			}
			this.factory = factory;
			this.endianness = endianness;
			this.buffer = factory.GetBuffer(this.Order(), estimatedLength);
		}

		// Token: 0x06000170 RID: 368 RVA: 0x00005000 File Offset: 0x00003200
		public override void EnsureWritableBytes(int minWritableBytes)
		{
			if (minWritableBytes <= base.WritableBytes())
			{
				return;
			}
			int i;
			if (this.Capacity() == 0)
			{
				i = 1;
			}
			else
			{
				i = this.Capacity();
			}
			int num = base.WriterIndex() + minWritableBytes;
			while (i < num)
			{
				i <<= 1;
			}
			MessageBuffer messageBuffer = this.Factory().GetBuffer(this.Order(), i);
			messageBuffer.WriteBytes(this.buffer, 0, base.WriterIndex());
			this.buffer = messageBuffer;
		}

		// Token: 0x06000171 RID: 369 RVA: 0x0000506A File Offset: 0x0000326A
		public override int Capacity()
		{
			return this.buffer.Capacity();
		}

		// Token: 0x06000172 RID: 370 RVA: 0x00005077 File Offset: 0x00003277
		public override void SetByte(int index, byte b)
		{
			this.buffer.SetByte(index, b);
		}

		// Token: 0x06000173 RID: 371 RVA: 0x00005086 File Offset: 0x00003286
		public override void SetShort(int index, short s)
		{
			this.buffer.SetShort(index, s);
		}

		// Token: 0x06000174 RID: 372 RVA: 0x00005095 File Offset: 0x00003295
		public override void SetInt(int index, int i)
		{
			this.buffer.SetInt(index, i);
		}

		// Token: 0x06000175 RID: 373 RVA: 0x000050A4 File Offset: 0x000032A4
		public override byte GetByte(int index)
		{
			return this.buffer.GetByte(index);
		}

		// Token: 0x06000176 RID: 374 RVA: 0x000050B2 File Offset: 0x000032B2
		public override short GetShort(int index)
		{
			return this.buffer.GetShort(index);
		}

		// Token: 0x06000177 RID: 375 RVA: 0x000050C0 File Offset: 0x000032C0
		public override int GetInt(int index)
		{
			return this.buffer.GetInt(index);
		}

		// Token: 0x06000178 RID: 376 RVA: 0x000050CE File Offset: 0x000032CE
		public override void GetBytes(int index, byte[] dst, int dstIndex, int length)
		{
			this.buffer.GetBytes(index, dst, dstIndex, length);
		}

		// Token: 0x06000179 RID: 377 RVA: 0x000050E0 File Offset: 0x000032E0
		public override void SetBytes(int index, byte[] src, int srcIndex, int length)
		{
			this.buffer.SetBytes(index, src, srcIndex, length);
		}

		// Token: 0x0600017A RID: 378 RVA: 0x000050F2 File Offset: 0x000032F2
		public override void SetBytes(int index, MessageBuffer src, int srcIndex, int length)
		{
			this.buffer.SetBytes(index, src, srcIndex, length);
		}

		// Token: 0x0600017B RID: 379 RVA: 0x00005104 File Offset: 0x00003304
		public override byte[] Array()
		{
			return this.buffer.Array();
		}

		// Token: 0x0600017C RID: 380 RVA: 0x00005111 File Offset: 0x00003311
		public override ByteOrder Order()
		{
			return this.endianness;
		}

		// Token: 0x0600017D RID: 381 RVA: 0x00005119 File Offset: 0x00003319
		public override MessageBufferFactory Factory()
		{
			return this.factory;
		}

		// Token: 0x0600017E RID: 382 RVA: 0x00005121 File Offset: 0x00003321
		public override void WriteByte(byte b)
		{
			this.EnsureWritableBytes(1);
			base.WriteByte(b);
		}

		// Token: 0x0600017F RID: 383 RVA: 0x00005131 File Offset: 0x00003331
		public override void WriteShort(short s)
		{
			this.EnsureWritableBytes(2);
			base.WriteShort(s);
		}

		// Token: 0x06000180 RID: 384 RVA: 0x00005141 File Offset: 0x00003341
		public override void WriteUshort(ushort us)
		{
			this.EnsureWritableBytes(2);
			base.WriteUshort(us);
		}

		// Token: 0x06000181 RID: 385 RVA: 0x00005151 File Offset: 0x00003351
		public override void WriteBytes(byte[] src, int srcIndex, int length)
		{
			this.EnsureWritableBytes(length);
			base.WriteBytes(src, srcIndex, length);
		}

		// Token: 0x06000182 RID: 386 RVA: 0x00005163 File Offset: 0x00003363
		public override void WriteBytes(MessageBuffer src, int srcIndex, int length)
		{
			this.EnsureWritableBytes(length);
			base.WriteBytes(src, srcIndex, length);
		}

		// Token: 0x0400005D RID: 93
		private readonly MessageBufferFactory factory;

		// Token: 0x0400005E RID: 94
		private readonly ByteOrder endianness;

		// Token: 0x0400005F RID: 95
		private MessageBuffer buffer;
	}
}
